# 使用 ccache
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
    set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
endif()

# 优先使用静态库，找不到时使用动态库
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")

# 基础编译标志
set(CMAKE_COMMON_FLAGS "-Wall -Wextra -Werror -Wshadow -Wdate-time -Wno-format-nonliteral")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")

# 设置 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 添加 GCC 版本检查
if(CMAKE_COMPILER_IS_GNUCXX)
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
        message(FATAL_ERROR "GCC version must be at least 7.0 for C++17 support!")
    endif()
endif()

# 基础编译标志
set(SOURCE_DATE_EPOCH $ENV{SOURCE_DATE_EPOCH})
if(NOT SOURCE_DATE_EPOCH)
    string(TIMESTAMP SOURCE_DATE_EPOCH "%s" UTC)
endif()

# 获取Git Hash
execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET
)
if(NOT GIT_HASH)
    set(GIT_HASH "unknown")
endif()

# 基础编译标志
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DSOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}")
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -DGIT_HASH=\\\"${GIT_HASH}\\\"")
set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wall -Wextra -Werror -Wshadow -Wdate-time -Wno-psabi -Wno-format-nonliteral")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")

# debug 模式下添加更多调试选项
if(CMAKE_BUILD_TYPE STREQUAL "debug")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -g -O1 -rdynamic -fstack-protector-all -fno-omit-frame-pointer -DDEBUG")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ftrapv -fno-inline -fno-eliminate-unused-debug-types")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fstack-check -fstack-protector-strong")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Wformat=2 -Wformat-security -Werror=format-security")
    # set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -D_FORTIFY_SOURCE=2")
    # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fsanitize-recover=address -static-libasan")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,relro,-z,now -rdynamic")
else()
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -Os -DNDEBUG")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ffunction-sections -fdata-sections")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -ffast-math -funroll-loops")
    set(CMAKE_COMMON_FLAGS "${CMAKE_COMMON_FLAGS} -fomit-frame-pointer")
    
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-O1")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/bin/)

# 提供统一的链接库
set(EL_SYS_LIBS pthread dl udev z crypto ssl curl freetype minizip-ng mongoose)
set(EL_BASE_LIBS base common)
set(EL_HAL_LIBS hal_init hal_common hal_net hal_audio hal_camera)
set(EL_FDN_LIBS event system_server stream stream_server web_server cloud_model storage record fdn_muxer upgrade restful protocol)
set(EL_ALL_LIBS -Wl,--start-group ${EL_SYS_LIBS} ${EL_CHIP_LIBS} ${EL_BASE_LIBS} ${EL_HAL_LIBS} ${EL_FDN_LIBS} -Wl,--end-group)
set(EL_ALL_TEST_LIBS -Wl,--start-group ${EL_SYS_LIBS} ${EL_CHIP_LIBS} ${EL_BASE_LIBS} ${EL_HAL_LIBS} ${EL_FDN_LIBS} gtest gtest_main -Wl,--end-group)